package org.example.kidscode.service.impl;

import org.example.kidscode.entity.User;
import org.example.kidscode.repository.UserRepository;
import org.example.kidscode.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    private final UserRepository userRepository;
    public UserServiceImpl(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
    @Override
    public User findByUsername(String username) {
        return userRepository.findByUsername(username);
    }
    @Override
    public boolean checkPassword(String username, String password) {
        User user = userRepository.findByUsername(username);
        return user != null && user.getPassword().equals(password);
    }
    @Override
    public boolean register(String username, String password) {
        if (userRepository.findByUsername(username) != null) {
            return false; // 用户已存在
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password); // 实际项目应加密
        userRepository.save(user);
        return true;
    }
}